package com.amorcloud.pig;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDex;

import com.amorcloud.pig.bean.UserInfoBean;
import com.amorcloud.pig.greendao.DaoMaster;
import com.amorcloud.pig.greendao.DaoSession;
import com.amorcloud.pig.utils.SharedPreferencesUtils;

public class MyApp extends Application {

    private static DaoSession daoSession;
    public static Context context;
    public static UserInfoBean currentUser;
    //public static String main_url = "http://q.danmch.com:9997";
    public static String main_url = "http://gyp.test.raybaca.cn:901";
    public static String lon;
    public static String lat;
    public static final String UHF_APP_KEY = "577d5abc59adc547a1c7ffaf8b5b7ec4cb12afc1";

    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
        context = this;
        setupDatabase();
    }

    /**
     * 配置数据库
     */
    private void setupDatabase() {
        //创建数据库shop.db
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "amor.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }

    public static boolean isAdmin() {
        if("1".equals(currentUser.getIsAdmin())) {
            return true;
        }
        return false;
    }

    public static String getUrl() {
        String navIp = (String) SharedPreferencesUtils.getParam("farmIp", "");
        String navPort = (String) SharedPreferencesUtils.getParam("farmPort", "");
        if(navIp.isEmpty() || navPort.isEmpty()){
            return main_url;
        } else {
            return "http://" + navIp + ":" + navPort;
        }
    }

}
